<template>
  <el-card class="box-card" style="margin:15px">
    <div id="dataaudit">
      <el-form ref="projectManageForm" :model="auditFormData" label-width="120px" size="small">
        <el-row style="margin-bottom:10px !important">
          <el-col :span="6">
            <el-form-item label="实验编号:">
              <el-input size="mini" v-model="queryData.expNum" placeholder="输入" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="母亲姓名:">
              <el-input size="mini" v-model="queryData.momName" placeholder="输入" />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="实验结果:">
              <el-select v-model="queryData.experimentValueResult.key">
                <el-option
                  :label="item"
                  :value="key"
                  v-for="(item, key, index) in dicts.EXPERIMENT_RESULT"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-button
              size="mini"
              type="primary"
              @click="clickResult"
              style="margin-left:15px  !important;"
            >重置</el-button>
          </el-col>
        </el-row>
        <el-row :span="24" style="margin-bottom:1vw !important;display:flex">
          <div v-for="(item,index) in hintList" :key="index" style="margin-left:1vw;">
            <span style="font-size: 0.8vw;font-weight: bold;">{{item.indicatorCodeName}}：</span>
            <span style="font-size:0.7vw">{{item.hintCount}}</span>
          </div>
        </el-row>
        <el-row>
          <el-table
            ref="dormitoryTable"
            row-key="sampleId"
            :data="tables"
            stripe
            style="width: 100%;"
            :header-cell-style="{
              background: '#333333',
              color: '#ffffff',
              textAlign: 'center'
            }"
            class="import-table"
            :cell-style="tableRowStyle"
          >
            <el-table-column align="center" type="index" width="65" />
            <el-table-column align="center" label="实验编号" width="150" prop="expNum" />
            <el-table-column align="center" label="alpha" width="80" prop="alpha-THA" />
            <el-table-column align="center" label="beta" width="80" prop="beta-THA" />
            <el-table-column align="center" label="M-Hb" width="80" prop="M-Hb" />
            <el-table-column align="center" label="条形码号" width="150" prop="barCode" />
            <el-table-column align="center" label="采血日期" width="100" prop="bloodTime">
              <template slot-scope="scope">
                <span>
                  {{
                  scope.row.bloodTime ? scope.row.bloodTime.substr(0, 10) : ""
                  }}
                </span>
              </template>
            </el-table-column>
            <el-table-column align="center" label="母亲姓名" prop="momName" width="100" />
            <el-table-column align="center" label="性别" prop="babySex.data" width="50" />
            <el-table-column align="center" label="出生日期" width="100" prop="birthTime">
              <template slot-scope="scope">
                <span>
                  {{
                  scope.row.birthTime ? scope.row.birthTime.substr(0, 10) : ""
                  }}
                </span>
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              label="所属医院"
              prop="hospitalId.data.label"
              show-overflow-tooltip
            />
            <el-table-column align="center" label="实验项目" width="80" prop="experimentCode.data" />
            <el-table-column align="center" label="实验日期" width="100" prop="experimentDate">
              <template slot-scope="scope">
                <span>
                  {{
                  scope.row.experimentDate
                  ? scope.row.experimentDate.substr(0, 10)
                  : ""
                  }}
                </span>
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              label="实验结果"
              width="100"
              prop="experimentValueResult.data"
            />
            <el-table-column align="center" label="实验数据" prop="experimentValue" width="80">
              <template slot-scope="scope">
                <span
                  style="color: #1890ff;cursor: pointer;"
                  @click="clickSeeExpValue(scope.row.experimentValue)"
                >点击查看</span>
              </template>
            </el-table-column>
            <el-table-column align="center" label="实验结论" prop="experimentResult.key">
              <template slot-scope="scope">
                <el-select
                  size="mini"
                  v-model="scope.row.experimentResult.key"
                  @change="changeExperimentResult(scope.row)"
                  placeholder="实验结论"
                >
                  <el-option
                    v-for="(item, key, index) in dicts.EXPERIMENT_RESULT"
                    :key="index"
                    :label="item"
                    :value="key"
                  />
                </el-select>
              </template>
            </el-table-column>
          </el-table>

          <el-row>
            <el-col :span="8">
              <el-form-item label="实验人员:" prop="experimentResultMan">
                <el-input
                  size="mini"
                  style="width:300px"
                  v-model="auditFormData.experimentResultMan"
                  placeholder="输入"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="审核人员:" prop="experimentCheckMan">
                <el-input
                  size="mini"
                  style="width:300px"
                  v-model="auditFormData.experimentCheckMan"
                  placeholder="输入"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="审核日期:" prop="experimentCheckTime">
                <el-date-picker
                  size="mini"
                  v-model="auditFormData.experimentCheckTime"
                  type="date"
                  style="width:300px"
                  value-format="yyyy-MM-dd"
                  placeholder="选择日期"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-col :span="24">
            <div style="display:flex;widht:100%;justify-content: center;margin-top:10px">
              <el-button size="small" type="primary" @click="clickAuditSeav">保存</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>

      <!-- 点击查看 -->
      <el-dialog width="65%" title="实验数据" :visible.sync="seeShow" append-to-body>
        <!-- 表格 -->
        <el-row style="line-height: 1.5;border:1px solid">
          <el-col
            :span="5"
            style="display:flex;justify-content: center;color:#000000f2;font-size: 15px;border-right:1px solid"
          >筛查项目</el-col>
          <el-col
            :span="3"
            style="display:flex;justify-content: center;color:#000000f2;font-size: 15px;border-right:1px solid"
          >检测指标</el-col>
          <el-col
            :span="3"
            style="display:flex;justify-content: center;color:#000000f2;font-size: 15px;border-right:1px solid"
          >检测值</el-col>
          <el-col
            :span="5"
            style="display:flex;justify-content: center;color:#000000f2;font-size: 15px;border-right:1px solid"
          >正常参考值</el-col>
          <el-col
            :span="8"
            style="display:flex;justify-content: center;color:#000000f2;font-size: 15px;"
          >筛查结果</el-col>
          <!-- hint -->
        </el-row>
        <!-- 表格内容 -->
        <el-row
          style="line-height: 1.5;border-bottom:1px solid;border-left:1px solid;border-right:1px solid;"
        >
          <div v-for="(item, index) in seeData" :key="index">
            <el-col
              :span="5"
              :style="item.experimentValueResult != 'normal' ? 'background-color:#baccef;':''"
              style="display:flex;justify-content: center;color:#000000f2;font-size: 15px;border-right:1px solid"
            >{{ item.indicatorCodeName }}&nbsp</el-col>
            <el-col
              :span="3"
              :style="item.experimentValueResult != 'normal' ? 'background-color:#baccef;':''"
              style="display:flex;justify-content: center;color:#000000f2;font-size: 15px;border-right:1px solid"
            >{{ item.indicatorCode }}&nbsp</el-col>
            <el-col
              :span="3"
              :style="item.experimentValueResult != 'normal' ? 'background-color:#baccef;':''"
              style="display:flex;justify-content: center;color:#000000f2;font-size: 15px;border-right:1px solid"
            >{{ item.experimentValue }}{{ item.units }}&nbsp</el-col>
            <el-col
              :span="5"
              :style="item.experimentValueResult != 'normal' ? 'background-color:#baccef;':''"
              style="display:flex;justify-content: center;color:#000000f2;font-size: 15px;border-right:1px solid"
            >{{ item.referenceValue }}&nbsp</el-col>
            <el-col
              :span="8"
              :style="item.experimentValueResult != 'normal' ? 'background-color:#baccef;':''"
              style="display:flex;justify-content: center;color:#000000f2;font-size: 15px;border-right:1px solid"
            >{{ item.hint }}&nbsp</el-col>
          </div>
        </el-row>
      </el-dialog>
    </div>
  </el-card>
</template>

<script>
import DeliveryManageApi from "@/api/DeliveryManage.js";
import { initDicts } from "@/utils/commons";

export default {
  components: {},
  data() {
    return {
      // 转菊花
      loading: false,
      // 点击查看
      seeShow: false,
      // 审核弹窗
      auditShow: false,
      // 打印显示
      printShow: false,
      // 查看数据
      seeData: [],
      // 页条数
      pageSize: 10,
      // 页数
      pageIndex: 1,
      // 表格数据
      tableData: [],
      // 选中数据
      selectList: [],
      // 查询数据
      basicData: {
        current: 1,
        map: {},
        model: {
          batchNumber: "",
          experimentCode: {
            data: "",
            key: ""
          },
          flowStatus: {
            data: "",
            key: ""
          },
          qualityControl: "",
          reagent1: "",
          reagent2: "",
          swatchId: {
            key: ""
          },
          swatchMan: "",
          swatchTimeStartEnd: {
            end: "",
            start: ""
          }
        },
        order: "descending",
        size: 10,
        sort: "id"
      },
      // 总数
      total: 0,
      // 字典
      dicts: {
        EXPERIMENT_CODE: {}, //实验项目
        EXPERIMENT_RESULT: {} //实验结论
      },
      // 审核弹窗数据
      auditList: [],
      user: this.$store.state.account.user.name,
      // 审核提交数据
      auditFormData: {
        experimentCode: {
          data: "",
          key: ""
        },
        experimentSwatchId: "",
        list: [],
        experimentResultMan: this.$store.state.account.user.name,
        experimentCheckMan: this.$store.state.account.user.name,
        experimentReportMan: "",
        experimentResultTime: "",
        experimentCheckTime: "",
        experimentReportTime: ""
      },
      // 打印数据
      printData: {
        dto: {
          batchNumber: "",
          swatchId: {
            key: ""
          },
          swatchMan: "",
          swatchTime: "",
          qualityControl: "",
          experimentCode: "",
          sampleCount: ""
        }
      },
      // 审核查询数据
      queryData: {
        expNum: "",
        momName: "",
        experimentValueResult: {
          key: "",
          data: ""
        }
      },
      // 布板数据··············
      // 复检样本
      queryRecheckData: [],
      // 正常样本
      queryNormalData: [],
      // 复检样本转菊花
      queryRechecLoading: false,
      // 正常样本
      queryNormal: [],
      // 正常样本转菊花
      queryNormalLoading: false,
      // 布板弹窗
      bubanShow: false,
      // 正常样本表格转菊花
      importLoading: false,
      // 打印显示
      printShow: false,
      // 布板保存数据
      bubanAddForm: {
        direction: {
          data: "",
          key: ""
        },
        batchNumber: "",
        experimentCode: {
          key: ""
        },
        qualityControl: {
          key: ""
        },
        reagent1: {
          key: ""
        },
        reagent2: {
          key: ""
        },
        swatchId: {
          key: ""
        },
        swatchMan: this.$store.state.account.user.name,
        swatchTime:
          new Date().getFullYear().toString() +
          "-" +
          ((new Date().getMonth() + 1).toString() < 10
            ? "0" + (new Date().getMonth() + 1).toString()
            : (new Date().getMonth() + 1).toString()) +
          "-" +
          (new Date().getDate().toString() < 10
            ? "0" + new Date().getDate().toString()
            : new Date().getDate().toString()),
        swatchValue: []
      },
      // 板验证
      bubanAddFormRules: {
        batchNumber: [{ required: true, message: "   ", trigger: "change" }],
        "swatchId.key": [{ required: true, message: "   ", trigger: "change" }],
        swatchMan: [{ required: true, message: "   ", trigger: "change" }],
        swatchTime: [{ required: true, message: "   ", trigger: "change" }],
        "reagent1.key": [{ required: true, message: "   ", trigger: "change" }],
        "qualityControl.key": [
          { required: true, message: "   ", trigger: "change" }
        ]
      },
      // 板本地操作数据
      banItem: {
        selectW: "",
        barcode: "",
        startCode: "",
        endCode: "",
        selectCode: ""
      },
      // 布板方案
      swatchList: [],
      // 试剂
      queryReagentList: [],
      // 质控品
      queryQualityList: [],
      // 布板数据
      banData: [],
      // 板数量
      banTotal: 0,
      // 显示布板
      banShow: false,
      // 当前点击行
      clickRow: [],
      // 选中的编码
      codeList: [],
      // 布板列数
      rowList: [],
      // 布板行数
      columnList: [],
      // 表格方向
      direction: {
        data: "",
        key: "" // vertical
      },
      rowNum: 0,
      colNum: 0,
      hintList: [] //审核统计
    };
  },
  computed: {
    // 模糊搜索
    tables() {
      if (this.auditList.length > 0) {
        let dataList = JSON.parse(JSON.stringify(this.auditList));
        let queryList = [];
        if (this.queryData.expNum != "") {
          let i = 0;
          dataList.filter(data => {
            Object.keys(data).some(key => {
              if (key == "expNum") {
                if (
                  String(data[key])
                    .toLowerCase()
                    .indexOf(this.queryData.expNum) > -1
                ) {
                  queryList.push(data);
                  i++;
                }
              }
            });
          });
          if (i == 0) {
            this.$message.error("没有当前实验编号");
          }
        } else if (this.queryData.momName != "") {
          dataList.filter(data => {
            Object.keys(data).some(key => {
              if (key == "momName") {
                if (
                  String(data[key])
                    .toLowerCase()
                    .indexOf(this.queryData.momName) > -1
                ) {
                  queryList.push(data);
                }
              }
            });
          });
        } else if (this.queryData.experimentValueResult.key != "") {
          dataList.filter(data => {
            Object.keys(data).some(key => {
              if (key == "experimentValueResult") {
                if (
                  String(data[key].key)
                    .toLowerCase()
                    .indexOf(this.queryData.experimentValueResult.key) > -1
                ) {
                  queryList.push(data);
                }
              }
            });
          });
        } else {
          queryList = JSON.parse(JSON.stringify(this.auditList));
        }
        //设置标题，alpha，beta，M-Hb
        queryList.forEach((item, index) => {
          if (item.experimentCode.key == "THA") {
            if (item.experimentValue) {
              for (let key in item.experimentValue) {
                queryList[index][key] =
                  item.experimentValue[key].experimentValue +
                  item.experimentValue[key].units;
              }
            }
          }
        });

        return queryList;
      }
    }
  },
  mounted() {
    initDicts(["EXPERIMENT_CODE", "EXPERIMENT_RESULT"], this.dicts);
    this.getList(this.basicData);
  },
  methods: {
    // 修改实验结论
    changeExperimentResult(row) {
      this.auditList.forEach((item, index) => {
        if (item.sampleId == row.sampleId) {
          this.$set(this.auditList, index, row);
        }
      });
    },
    // 重置搜索
    clickResult() {
      this.queryData = {
        expNum: "",
        momName: "",
        experimentValueResult: {
          key: "",
          data: ""
        }
      };
    },
    // 设置单元格样式
    tableRowStyle({ row, rowIndex }) {
      if (row.experimentValueResult.key != "normal") {
        return { "background-color": "#baccef", color: "#606266" };
      }
    },
    // 过滤去重(获取布板数)
    filBanList(arr) {
      let data = [];
      arr.forEach(item => {
        data.push(item.ban);
      });
      return Array.from(new Set(data));
    },
    // 过滤去重(获取布板列数)
    filRowList(arr) {
      let data = [];
      arr.forEach(item => {
        data.push(item.col);
      });
      return Array.from(new Set(data));
    },
    // 过滤去重(获取布板列数)
    filColumnList(arr) {
      let data = [];
      arr.forEach(item => {
        data.push(
          this.direction.key == "horizontal" ? item.r : item.pos.split("-")[1]
        );
      });
      return Array.from(new Set(data));
    },
    // 点击打印
    clickPrint() {
      if (this.selectList.length == 0) {
        this.$message.error("请选择要打印的数据！");
        return;
      }
      if (this.selectList.length > 1) {
        this.$message.error("只能选择一条数据打印！");
        return;
      }
      let rowList = [];
      let columnList = [];
      let data = {};
      this.filColumnList(this.selectList[0].swatchValue).forEach(
        (item, index) => {
          columnList[index] = {
            id:
              this.selectList[0].direction.key == "horizontal"
                ? item
                : this.$rowData[index],
            type: ""
          };
        }
      );
      if (columnList[0].type != "banIndex") {
        columnList.unshift({ id: "1", type: "banIndex" });
      }
      rowList = this.filRowList(this.selectList[0].swatchValue); //根据布板数组过滤行数

      data = {
        dto: this.selectList[0],
        columnList: columnList,
        rowList: rowList,
        direction: this.selectList[0].direction,
        banTotal: this.filBanList(this.selectList[0].swatchValue).length,
        tableList: this.selectList[0].swatchValue
      };
      this.printData = data;
      this.printShow = true;
    },
    // 点击查看
    clickSeeExpValue(v) {
      this.seeShow = true;
      let sortData = [];
      for (let key in v) {
        sortData.push(v[key]);
      }
      this.seeData = this.$keysrt(sortData, "sortValue");
    },
    // 选择数据
    clickDelete() {
      if (this.selectList.length == 0) {
        this.$message.error("请选择要删除的数据");
        return;
      }
      // 加删除验证
      this.$confirm(`是否删除${this.selectList.length}条数据?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.selectList.forEach(item => {
          DeliveryManageApi.dataAudit
            .delete({
              "ids[]": item.id
            })
            .then(res => {
              this.getList(this.basicData);
            });
        });
      });
    },
    // 审核保存
    clickAuditSeav() {
      this.auditList.forEach(item => {
        item.experimentCheckStatus = "Y";
        item.experimentResultTime = this.auditFormData.experimentCheckTime;
        item.experimentCheckTime = this.auditFormData.experimentCheckTime;
        item.experimentReportTime = this.auditFormData.experimentReportTime;
        item.experimentCheckMan = this.auditFormData.experimentCheckMan;
        item.experimentReportMan = this.auditFormData.experimentReportMan;
        item.experimentResultMan = this.auditFormData.experimentResultMan;
      });
      this.auditFormData.list = this.auditList;
      DeliveryManageApi.dataAudit.auditSave(this.auditFormData).then(res => {
        if (res.status == 200) {
          this.$message({
            type: "success",
            message: res.statusText
          });
          this.getList(this.basicData);
          this.auditShow = false;
        } else {
          this.$message.error(res.statusText);
        }
      });
    },
    // 点击审核
    clickAudit() {
      if (this.selectList.length > 1) {
        this.$message.error("只能选择一条数据！");
        return;
      }
      if (this.selectList.length == 0) {
        this.$message.error("请选择一条数据！");
        return;
      }
      this.auditShow = true;
      this.auditFormData.experimentCode = this.selectList[0].experimentCode;
      this.auditFormData.experimentSwatchId = this.selectList[0].id;
      let date =
        new Date().getFullYear().toString() +
        "-" +
        ((new Date().getMonth() + 1).toString() < 10
          ? "0" + (new Date().getMonth() + 1).toString()
          : (new Date().getMonth() + 1).toString()) +
        "-" +
        (new Date().getDate().toString() < 10
          ? "0" + new Date().getDate().toString()
          : new Date().getDate().toString());
      this.auditFormData.experimentResultTime = date;
      this.auditFormData.experimentCheckTime = date;

      DeliveryManageApi.dataAudit
        .auditView({
          experimentSwatchId: this.selectList[0].id
        })
        .then(res => {
          res.data.data.forEach(item => {
            if (item.experimentResult.key == null) {
              item.experimentResult.key = "normal";
            }
          });
          this.auditList = res.data.data;
        });
      DeliveryManageApi.dataAudit
        .auditInfo({
          experimentSwatchId: this.selectList[0].id
        })
        .then(res => {
          console.log(res.data.data);

          this.hintList = res.data.data.hintList;
          let date =
            new Date().getFullYear().toString() +
            "-" +
            ((new Date().getMonth() + 1).toString() < 10
              ? "0" + (new Date().getMonth() + 1).toString()
              : (new Date().getMonth() + 1).toString()) +
            "-" +
            (new Date().getDate().toString() < 10
              ? "0" + new Date().getDate().toString()
              : new Date().getDate().toString());

          this.auditFormData.experimentResultMan = res.data.data.result
            .experimentResultMan
            ? res.data.data.result.experimentResultMan
            : this.$store.state.account.user.name;
          this.auditFormData.experimentCheckMan = res.data.data.result
            .experimentCheckMan
            ? res.data.data.result.experimentCheckMan
            : this.$store.state.account.user.name;
          this.auditFormData.experimentCheckTime = res.data.data.result
            .experimentCheckTime
            ? res.data.data.result.experimentCheckTime
            : date;
        });
    },
    // 获取数据
    getList(data) {
      this.loading = true;
      DeliveryManageApi.dataAudit
        .page(data)
        .then(res => {
          if (res.status === 200) {
            this.tableData = res.data.data.records;
            this.total = res.data.data.total;
          } else {
            this.$message.error(res.statusText);
          }
        })
        .finally(() => (this.loading = false));
    },
    //点击全选
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 点击分页
    handleSizeChange: function(val) {
      this.basicData.size = val;
      this.getList(this.basicData);
    },
    // 选择页码
    handleCurrentChange: function(currentPage) {
      this.basicData.current = currentPage;
      this.getList(this.basicData);
    },
    // 选中数据操作
    selectChanage(v) {
      this.selectList = v;
    }
  }
};
</script>

<style lang="less" >
#dataaudit {
}
#experimentData-dialog {
  #sxtb {
    td {
      padding: 5px 0 !important;
    }
    th {
      padding: 5px 0 !important;
    }
  }
  display: flex;
  justify-content: center;
  align-items: center;
  .el-dialog {
    margin-top: 0 !important;
  }
  .el-message-box__message {
    width: 280px;
    overflow: hidden;
    white-space: normal;
    word-break: break-all;
  }
  #ban-table-tow {
    .el-table__body-wrapper {
      height: 200px !important;
      overflow-y: auto;
    }
  }
  .el-dialog__title {
    color: #fff;
  }
  .el-form-item__content {
    width: 80%;
    .el-select {
      width: 100%;
    }
    .el-date-editor {
      width: 100%;
    }
  }
}
</style>
